import React from 'react';
import { observer, inject } from 'mobx-react';
import { WeaTools,WeaErrorPage,WeaRightMenu,WeaTop,WeaTab,WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import { Button,Modal } from 'antd';
import { WeaTableNew } from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
import allStore from '../../stores';
import Condition from './Condition';
import { splitPrintModeInfo } from '../../apis/batchPrint';

@inject("wfBatchPrintResultStore")
@observer
class PrintResult extends React.Component{
    componentDidMount(){
        this.doInit(this.props);
        const { wfBatchPrintResultStore } = this.props;
        if(window.location.pathname.indexOf('/spa/workflow/index') > -1)
            document.title = wfBatchPrintResultStore.title;
    }
    doInit(props){
        const { wfBatchPrintResultStore,location } = props;
        wfBatchPrintResultStore.reset();
        const urlParams = location.query || {};
        wfBatchPrintResultStore.setUrlParams(urlParams);
        const fromwhere = urlParams.fromwhere || "index";
        if(fromwhere === "index"){     //来源导航界面搜索
            wfBatchPrintResultStore.copyIndexStore(allStore.wfBatchPrintIndexStore);
        }
        wfBatchPrintResultStore.doSearch();
    }
    componentWillUnmount(){
        const { wfBatchPrintResultStore } = this.props;
        wfBatchPrintResultStore.reset();
    }

    varTemp = {
      collectParams: {favname:'', favouritetype:2}
    }
    setShowSearchAd= bool=>{this.props.wfBatchPrintResultStore.setShowSearchAd(bool)};
    hideSearchAd=()=>this.props.wfBatchPrintResultStore.setShowSearchAd(false);
    onSearch= v=>{ this.props.wfBatchPrintResultStore.doSearch()}
    render(){
        const { wfBatchPrintResultStore } = this.props;
        const { loading,title,fixedForm,showSearchAd,tableStore } = wfBatchPrintResultStore;
        const fixedFormParams = fixedForm && fixedForm.getFormParams() || {};
        this.varTemp.collectParams.favname = title;
        return (
            <div className="wf-print-result">
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@yeowb8`} 
                    collectParams={this.varTemp.collectParams}
                    datas={this.getRightMenu()}
                >
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@490p4k`}
                        title={title}
                        loading={loading || tableStore.loading}
                        icon={<i className='icon-coms-workflow' />}
                        iconBgcolor='#0079DE'
                        buttons={this.getTopButtons()}
                        buttonSpace={10}
                        showDropIcon={true}
                        dropMenuDatas={this.getRightMenu()}
                    >
                        <div style={{height: '100%'}}>
                            <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@fje74e`}
                                buttonsAd={this.getAdButtons()}
                                searchType={['base','advanced']}
                                searchsBaseValue={fixedFormParams.requestname}
                                setShowSearchAd={this.setShowSearchAd}
                                hideSearchAd={this.hideSearchAd}
                                searchsAd={
                                    <Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@ap4rk3`} store={wfBatchPrintResultStore} onEnterSearch={this.onSearch} />
                                }
                                showSearchAd={showSearchAd}
                                onSearch={this.onSearch}
                                onSearchChange={v=>{wfBatchPrintResultStore.appendFixedFormFields({requestname:{value:v}})}}
                            />
                            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@t7dh7l`}
                                comsWeaTableStore={tableStore}
                                hasOrder={true}
                            />
                        </div>
                    </WeaTop>
                </WeaRightMenu>
            </div>
        )
    }
    getTopButtons(){
        const { wfBatchPrintResultStore } = this.props;
        const { tableStore } = wfBatchPrintResultStore;
        const selectedRowKeys = tableStore.selectedRowKeys;
        let btns =[];
        btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qrtu3t@batchPrint`} type="primary" disabled={selectedRowKeys.length <= 0} onClick={this.batchPrintClick}>{getLabel(26382,'批量打印')}</Button>);
        btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@4h9yen@changehadPrintStatus`} disabled={selectedRowKeys.length <= 0} onClick={this.changePrintStatus.bind(this,1)}>{getLabel(84820,'设为已打印')}</Button>);
        btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@emqk52@$changeunPrintStatus`} disabled={selectedRowKeys.length <= 0} onClick={this.changePrintStatus.bind(this,0)}>{getLabel(84821,'设为未打印')}</Button>);
        //btns.push(<Button onClick={this.showPrintLog.bind(this)}>{getLabel(-1,'打印日志')}</Button>);
        return btns;
    }
    getRightMenu(){
        const { wfBatchPrintResultStore } = this.props;
        const { tableStore } = wfBatchPrintResultStore;
        const selectedRowKeys = tableStore.selectedRowKeys;
    	let btns = [];
    	btns.push({
            key: "search",
    		icon: <i className='icon-workflow-Right-menu--search'/>,
            content: getLabel(197,'搜索'),
            onClick: this.onSearch
        });
        btns.push({
            key: "batchPrint",
            icon: <i className='icon-workflow-Right-menu-batch'/>,
            content: getLabel(26382,'批量打印'),
            disabled: selectedRowKeys.length <= 0,
            onClick: this.batchPrintClick
        });
        btns.push({
            key: "setPrint",
            icon: <i className='icon-coms-Browse-box-add-all'/>,
            content: getLabel(84820,'设为已打印'),
            disabled: selectedRowKeys.length <= 0,
            onClick: this.changePrintStatus.bind(this,1)
        });
        btns.push({
            key: "setUnPrint",
            icon: <i className='icon-coms-Browse-box-Delete-all'/>,
            content: getLabel(84821,'设为未打印'),
            disabled: selectedRowKeys.length <= 0,
            onClick: this.changePrintStatus.bind(this,0)
        });
        // btns.push({
        //     key: "showLog",
        //     icon: <i className='icon-coms-Print-log'/>,
        //     content: getLabel(-1,'打印日志'),
        //     onClick: this.showPrintLog.bind(this)
        // });
        btns.push({
            key: "colSet",
    		icon: <i className='icon-workflow-Right-menu-Custom'/>,
            content: getLabel(32535,'显示列定制'),
            onClick: key =>{
                tableStore.setColSetVisible(true);
    		        tableStore.tableColSet(true);
            }
    	});
    	return btns;
    }
    reset = ()=>{this.props.wfBatchPrintResultStore.resetCondition()}
    cancel = ()=>{this.props.wfBatchPrintResultStore.setShowSearchAd(false)}
    getAdButtons() {
        const { wfBatchPrintResultStore } = this.props;
        return [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@jsj4v1@search`} type="primary" onClick={this.onSearch}>{getLabel(197,'搜索')}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@npxhww@reset`} type="ghost" onClick={this.reset}>{getLabel(2022,'重置')}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@m4o4tj@cancel`} type="ghost" onClick={this.cancel}>{getLabel(31129,'取消')}</Button>)
        ];
    }
    batchPrintClick = () => {
        const params = {requestids: this.getCheckedRequestids()};
        splitPrintModeInfo(params).then(datas =>{
            const { modeInfo,htmlInfo } = datas;
            if(modeInfo && modeInfo.length > 0){
                let printstr = "";
                modeInfo.map(item =>{
                    printstr += `,${item.requestid}|${item.modeid}|${item.workflowid}|${item.nodeid}`;
                });
                if(printstr.length > 0)
                    printstr = printstr.substring(1);
                window.openFullWindowHaveBar(`/workflow/multiprint/MultiPrintMode.jsp?printstr=${printstr}`);
            }
            if(htmlInfo && htmlInfo.length > 0){
                let printstr = "";
                htmlInfo.map(item =>{
                    printstr += `,${item.requestid}|${item.modeid}`;
                });
                if(printstr.length > 0)
                    printstr = printstr.substring(1);
                window.openFullWindowHaveBar(`/spa/workflow/index_form.jsp#/main/workflow/printReq?isBatch=1&printstr=${printstr}`);
            }
            const { wfBatchPrintResultStore } = this.props;
            window.setTimeout(function(){
                wfBatchPrintResultStore.reLoad();
            },1000)
        });
    }
    changePrintStatus = (status) => {
        const params = {requestids: this.getCheckedRequestids(), ismultiprint:status};
        const { wfBatchPrintResultStore } = this.props;
        wfBatchPrintResultStore.doChangePrintStatus(params);
    }
    getCheckedRequestids = () => {
        let requestids = "";
        const { wfBatchPrintResultStore } = this.props;
        const { tableStore } = wfBatchPrintResultStore;
        const selectedRowKeys = tableStore && tableStore.selectedRowKeys;
        selectedRowKeys && selectedRowKeys.map(key =>{
            requestids += `${key},`;
        });
        return requestids;
    }
    showPrintLog(){
        alert("wait a moment");
    }
}

export default WeaTools.tryCatch(React, 
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@8ir2xo`} msg={ props.error ? props.error : getLabel(383324,' 对不起，该页面异常，请联系管理员 ')+'！'} />,
    {error: ""}
)(PrintResult);